import { request } from '../request';

/** get role list */
export function fetchGetRoleList(params?: Api.SystemManage.RoleSearchParams) {
  return request<Api.SystemManage.RoleList>({
    url: '/role/page',
    method: 'get',
    params
  });
}

/** get role by id */
export function fetchGetRole(id: number) {
  return request<Api.SystemManage.Role>({
    url: '/role/detail',
    method: 'get',
    params: { id }
  });
}

/** create role */
export function fetchCreateRole(data: Api.SystemManage.Role) {
  return request<Api.SystemManage.Role>({
    url: '/role/add',
    method: 'post',
    data
  });
}

/** update role */
export function fetchUpdateRole(_id: number, data: Api.SystemManage.Role) {
  return request<Api.SystemManage.Role>({
    url: '/role/update',
    method: 'post',
    data
  });
}

/** delete role */
export function fetchDeleteRole(id: number) {
  return request({
    url: `/role/${id}`,
    method: 'delete'
  });
}

/** batch delete roles */
export function fetchBatchDeleteRoles(ids: number[]) {
  return request({
    url: '/role/batch',
    method: 'delete',
    data: ids
  });
}

/**
 * get all roles
 *
 * these roles are all enabled
 */
export function fetchGetAllRoles() {
  return request<Api.SystemManage.AllRole[]>({
    url: '/role/all',
    method: 'get'
  });
}

/** get role menu permissions */
export function fetchGetRoleMenus(roleId: number) {
  return request<{ code: string; msg: string; data: number[] }>({
    url: `/role/menus/${roleId}`,
    method: 'get'
  });
}

/** update role menu permissions */
export function fetchUpdateRoleMenus(roleId: number, menuIds: number[]) {
  console.info('fetchUpdateRoleMenus', roleId);
  return request<{ code: string; msg: string }>({
    url: `/role/menus/${roleId}`,
    method: 'post',
    data: menuIds
  });
}

/** get role button permissions */
export function fetchGetRoleButtons(roleId: number) {
  return request<{ code: string; msg: string; data: string[] }>({
    url: `/role/buttons/${roleId}`,
    method: 'get'
  });
}

/** update role button permissions */
export function fetchUpdateRoleButtons(roleId: number, buttonCodes: string[]) {
  console.info('fetchUpdateRoleButtons', roleId);
  return request<{ code: string; msg: string }>({
    url: `/role/buttons/${roleId}`,
    method: 'post',
    data: buttonCodes
  });
}

/** get role menu button permissions */
export function fetchGetRoleMenuButtons(roleId: number, menuId: number) {
  return request<{ code: string; msg: string; data: string[] }>({
    url: `/role/${roleId}/menu/${menuId}/buttons`,
    method: 'get'
  });
}

/** update role menu button permissions */
export function fetchUpdateRoleMenuButtons(roleId: number, menuId: number, buttonCodes: string[]) {
  console.info('fetchUpdateRoleMenuButtons', roleId, menuId);
  return request<{ code: string; msg: string }>({
    url: `/role/${roleId}/menu/${menuId}/buttons`,
    method: 'post',
    data: buttonCodes
  });
}

/** get role menu button tree (integrated) */
export function fetchGetRoleMenuButtonTree(roleId: number) {
  return request<{
    menuTree: any[];
    checkedMenuKeys: string[];
    buttonMap: Record<string, string[]>;
  }>({
    url: `/role/${roleId}/menu-button-tree`,
    method: 'get'
  });
}

/** update role menu button permissions (integrated) */
export function fetchUpdateRoleMenuButtonPermissions(
  roleId: number,
  data: {
    menuIds: string[];
    buttonMap: Record<string, string[]>;
  }
) {
  return request<{ code: string; msg: string }>({
    url: `/role/${roleId}/menu-button-permissions`,
    method: 'post',
    data
  });
}

/** get all button permissions */
export function fetchGetAllButtons() {
  return request<{ code: string; msg: string; data: Api.SystemManage.ButtonPermission[] }>({
    url: '/systemManage/getAllButtons',
    method: 'get'
  });
}

/** get user list */
export function fetchGetUserList(params?: Api.SystemManage.UserSearchParams) {
  return request<Api.SystemManage.UserList>({
    url: '/systemManage/getUserList',
    method: 'get',
    params
  });
}

/** get user by id */
export function fetchGetUser(id: number) {
  return request<Api.SystemManage.User>({
    url: `/user/${id}`,
    method: 'get'
  });
}

/** create user */
export function fetchCreateUser(data: Api.SystemManage.User) {
  return request<Api.SystemManage.User>({
    url: '/systemManage/user/add',
    method: 'post',
    data
  });
}

/** update user */
export function fetchUpdateUser(_id: number, data: Api.SystemManage.User) {
  return request<Api.SystemManage.User>({
    url: '/systemManage/user/update',
    method: 'post',
    data
  });
}

/** delete user */
export function fetchDeleteUser(id: number) {
  return request({
    url: `/systemManage/user/${id}`,
    method: 'delete'
  });
}

/** batch delete users */
export function fetchBatchDeleteUsers(ids: number[]) {
  return request({
    url: '/systemManage/user/batch',
    method: 'delete',
    data: ids
  });
}

/** get menu list */
export function fetchGetMenuList(params?: Api.SystemManage.RoleSearchParams) {
  return request<{ code: string; msg: string; data: Api.SystemManage.MenuList }>({
    url: '/systemManage/getMenuList/v2',
    method: 'get',
    params
  });
}

/** get menu tree */
export function fetchGetMenuTree() {
  return request<Api.SystemManage.MenuTree[]>({
    url: '/systemManage/getMenuTree',
    method: 'get'
  });
}

/** get menu by id */
export function fetchGetMenu(id: number) {
  return request<Api.SystemManage.Menu>({
    url: `/systemManage/menu/${id}`,
    method: 'get'
  });
}

/** create menu */
export function fetchCreateMenu(data: Api.SystemManage.Menu) {
  return request<Api.SystemManage.Menu>({
    url: '/systemManage/menu',
    method: 'post',
    data
  });
}

/** update menu */
export function fetchUpdateMenu(id: number, data: Api.SystemManage.Menu) {
  return request<Api.SystemManage.Menu>({
    url: `/systemManage/menu/${id}`,
    method: 'put',
    data
  });
}

/** delete menu */
export function fetchDeleteMenu(id: number) {
  return request({
    url: `/systemManage/menu/${id}`,
    method: 'delete'
  });
}

/** get all pages */
export function fetchGetAllPages() {
  return request<{ code: string; msg: string; data: string[] }>({
    url: '/systemManage/getAllPages',
    method: 'get'
  });
}

/** get dynamic menus for frontend */
export function fetchGetDynamicMenus() {
  return request<{ code: string; msg: string; data: Api.SystemManage.Menu[] }>({
    url: '/systemManage/getMenuTree',
    method: 'get'
  });
}

/** 获取日志列表，支持按level、logger、keyword、node过滤 */
export function fetchGetLogList(params?: {
  level?: string;
  logger?: string;
  keyword?: string;
  node?: string;
  limit?: number;
  offset?: number;
  order?: string;
  nodeIp?: string;
  nodePort?: string;
}) {
  const url = params?.nodeIp && params?.nodePort ? '/logs/proxy' : '/logs';
  return request({
    url,
    method: 'get',
    params
  });
}

/** 获取所有Logger列表 */
export function fetchGetLoggerList(params?: { nodeIp?: string; nodePort?: string }) {
  const url = params?.nodeIp && params?.nodePort ? '/logs/loggers/proxy' : '/logs/loggers';
  return request<string[]>({
    url,
    method: 'get',
    params
  });
}
